package com.uchiha.nova.mainService.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.uchiha.nova.core.model.entity.DictEntity;
import com.uchiha.nova.core.model.vo.DictVO;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.constant.NovaConstant;
import com.uchiha.nova.core.tool.support.BaseServiceImpl;
import com.uchiha.nova.core.tool.support.Condition;
import com.uchiha.nova.core.tool.util.CacheUtil;
import com.uchiha.nova.core.tool.util.Func;
import com.uchiha.nova.mainService.cache.CacheConstant;
import com.uchiha.nova.mainService.cache.DictCache;
import com.uchiha.nova.mainService.mapper.DictMapper;
import com.uchiha.nova.mainService.service.IDictService;
import com.uchiha.nova.mainService.wrapper.DictWrapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统字典 服务实现类
 *
 * @author Uchihashiamu
 */
@Service
public class DictServiceImpl extends BaseServiceImpl<DictMapper, DictEntity> implements IDictService {
    @Override
    public boolean deletePhysics(List<Long> ids) {
        ids.forEach(item-> this.baseMapper.deletePhysics(item));
        return true;
    }

    @Override
    public DictEntity getValue(String code, String dictKey) {
        DictEntity dict = this.baseMapper.selectOne(new QueryWrapper<DictEntity>().eq("code",code).eq("dict_key",dictKey));
        return dict;
    }

    @Override
    public R submit(DictEntity dict) {
        LambdaQueryWrapper<DictEntity> lqw = Wrappers.<DictEntity>query().lambda().eq(DictEntity::getCode, dict.getCode()).eq(DictEntity::getDictKey, dict.getDictKey());
        Long cnt = baseMapper.selectCount((Func.isEmpty(dict.getId())) ? lqw : lqw.notIn(DictEntity::getId, dict.getId()));
        if (cnt > 0L) {
            R.fail("当前字典键值已存在!");
        }
        // 修改顶级字典后同步更新下属字典的编号
        if (Func.isNotEmpty(dict.getId()) && dict.getParentId().longValue() == NovaConstant.TOP_PARENT_ID) {
            DictEntity parent = DictCache.getById(dict.getId());
            this.update(Wrappers.<DictEntity>update().lambda().set(DictEntity::getCode, dict.getCode()).eq(DictEntity::getCode, parent.getCode()).ne(DictEntity::getParentId, NovaConstant.TOP_PARENT_ID));
        }
        if (Func.isEmpty(dict.getParentId())) {
            dict.setParentId(NovaConstant.TOP_PARENT_ID);
        }
        dict.setDeleted(NovaConstant.DB_NOT_DELETED);
        if(ObjectUtil.isNotEmpty(dict.getId())){
            CacheUtil.clear(CacheConstant.DICT_CACHE,false);
        }

        return R.status(this.saveOrUpdate(dict));
    }

    @Override
    public List<DictVO> childList(DictEntity dict) {
        if (dict.getParentId().compareTo(0L)==-1) {
            return new ArrayList<>();
        }
        Long parentId = dict.getParentId();
        DictEntity parentDict = this.baseMapper.selectById(parentId);
        dict.setParentId(null);
        List<DictEntity> list = this.list(Condition.getQueryWrapper(dict).lambda().ne(DictEntity::getId, parentId).eq(DictEntity::getCode, parentDict.getCode()));
        return DictWrapper.build().listVO(list);
    }
}
